package net.monare.mifss.evn;

import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import net.monare.mifss.MifssMessages;
import net.monare.mifss.admin.ui.wiz.cards.StorageCard;
import net.monare.mifss.admin.ui.wiz.cards.StorageFinalizeCard;
import net.monare.mifss.admin.ui.wiz.cards.StoragePoolCard;
import net.monare.mifss.configurations.pojo.ApiKey;
import net.monare.mifss.configurations.pojo.ContentPoolDef;
import net.monare.mifss.configurations.pojo.StorageDef;
import org.apache.commons.lang.exception.ExceptionUtils;


public class CreateNewStorageListener extends BasicMifssListener implements ClickListener {

    private static final long serialVersionUID = 5490790603535223446L;

    private StorageCard storageCard;
    private StoragePoolCard storagePoolCard;
    private StorageFinalizeCard storageFinalizeCard;


    public CreateNewStorageListener(StoragePoolCard storagePoolCard, StorageCard storageCard, StorageFinalizeCard storageFinalizeCard) {

        this.storageCard = storageCard;
        this.storagePoolCard = storagePoolCard;
        this.storageFinalizeCard = storageFinalizeCard;

    }

    public void buttonClick(ClickEvent event) {

        // load  UI messages
        String apiKeyNotDefined = MifssMessages.getMessage("sys.error.api.key.not.defined");
        String poolDefNotDefined = MifssMessages.getMessage("sys.error.pool.def.not.defined");
        String storageDefNotDefined = MifssMessages.getMessage("sys.error.storage.def.not.defined");
        String storageNotCreated = MifssMessages.getMessage("sys.error.storage.not.created");
        String storageCreated = MifssMessages.getMessage("sys.success.storage.created");
        String errorOccurred = MifssMessages.getMessage("sys.error");

        // get definitions
        ContentPoolDef contentPoolDef = storagePoolCard.getContentPoolDef();
        StorageDef storageDef = storageCard.getStorageDef();
        String storageSetName = storageCard.getStorageSetName();
        ApiKey apiKey = storageFinalizeCard.getApiKey();


        // lock create storage button
        storageFinalizeCard.lockCreateStorageButton(true);

        if (contentPoolDef != null) {

            if (storageDef != null) {

                if (apiKey != null) {

                    // add pool definition to storage definition
                    storageDef.addContentPoolDef(contentPoolDef);

                    try {

                        // create new storage
                        getMifssHolder().getStorageService().createStorage(storageSetName, storageDef);

                        // create API key
                        getMifssHolder().getStorageService().createApiKey(storageDef.getName(), apiKey.getName(), apiKey.getDescription(), apiKey.getApiMode());

                        storageFinalizeCard.writeMessageLine(storageCreated);

                    } catch (Exception x) {

                        storageFinalizeCard.writeMessageLine(errorOccurred);
                        storageFinalizeCard.writeMessageLine(ExceptionUtils.getStackTrace(x));

                    }finally {
                        // unlock create storage button
                        storageFinalizeCard.lockCreateStorageButton(false);
                    }



                    return;

                } else {
                    storageFinalizeCard.writeMessageLine(apiKeyNotDefined);
                }

            } else {
                storageFinalizeCard.writeMessageLine(storageDefNotDefined);
            }

        } else {
            storageFinalizeCard.writeMessageLine(poolDefNotDefined);
        }

        storageFinalizeCard.writeMessageLine("...");
        storageFinalizeCard.writeMessageLine(storageNotCreated);

        // unlock create storage button
        storageFinalizeCard.lockCreateStorageButton(false);
    }

}
